package com.hui.controller;

import com.hui.entity.*;
import com.hui.service.MenuService;
import com.hui.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/08/11:16
 * @Description:
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 角色列表查询&条件查询
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {
        List<Role> list = roleService.getRoleByCondition(role);
        return new ResponseResult(true, 200, "查询成功", list);
    }

    // 添加&修改角色
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {
        if (role.getId() == null) {
            roleService.saveRole(role);
            return new ResponseResult(true, 200, "添加成功", null);
        } else {
            roleService.updateRole(role);
            return new ResponseResult(true, 200, "修改成功", null);
        }
    }

    @Autowired
    private MenuService menuService;

    // 查询所有菜单列表
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> list = menuService.getAllMenuByTree();
        Map<String, List> map = new HashMap<>();
        map.put("parentMenuList", list);
        return new ResponseResult(true, 200, "查询成功", map);
    }

    // 根据角色ID查询所关联的菜单ID
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {
        List<Integer> list = roleService.getMenuByRoleId(roleId);
        return new ResponseResult(true, 200, "查询成功", list);
    }

    // 为角色分配菜单列表
    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {
        roleService.roleContextMenu(roleMenuVo);
        return new ResponseResult(true, 200, "分配角色成功", null);
    }

    // 删除角色
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {
        roleService.deleteRole(id);
        return new ResponseResult(true, 200, "删除成功", null);
    }

    //  获取当前角色拥有的 资源信息
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> list = roleService.getResourceListByRoleId(roleId);
        return new ResponseResult(true, 200, "查询成功", list);
    }

    // 角色分配资源
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo resourceVo) {
        roleService.roleContextResource(resourceVo);
        return new ResponseResult(true, 200, "分配资源成功", null);
    }


}
